package com.jiaxiaobang.majiang.majiang.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

public class SimpleClientHandler extends ChannelInboundHandlerAdapter {

    private String equipmentId;

    public SimpleClientHandler(String equipID){
        this.equipmentId = equipID;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf= (ByteBuf) msg;
        String message = buf.toString(CharsetUtil.UTF_8);
        System.out.println("服务器端返回的数据:" + message);
        JSONObject messageJson = JSON.parseObject(message);
        String type = messageJson.getString("type");
        JSONObject resultMsg = new JSONObject();
        resultMsg.put("equipID",equipmentId);
        resultMsg.put("taskType",type);
        resultMsg.put("state","success");
        resultMsg.put("responseType","Operation");
        ctx.writeAndFlush(resultMsg.toJSONString());
    }
}
